package com.kkd.kkdapp.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.RepaymentHistoryAdapter;
import com.kkd.kkdapp.http.entity.CalendarListData;
import com.kkd.kkdapp.http.entity.CalendarListDataItem;
import com.kkd.kkdapp.http.entity.RepaymentHistoryDataBean;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class RepaymentHistoryView extends RelativeLayout implements
		KKDScrollListener {

	private Context mContext;

	private final int CODE_ZERO = 1;
	private final int CODE_ELSE = 2;
	private int pageNO = 1;
	private int pageSize = 20;
	private int pageCount;
	private boolean REFRESHFLAG = true;
	private boolean isShowToastOnRefresh = false;
	private KkdXlistView mXListView;
	private ImageView mNullImg;
	private TextView mNullTv;
	private ListView mListView;
	private RepaymentHistoryAdapter mAdapter;
	/**
	 * adapter所需要的bean集合
	 */
	private List<RepaymentHistoryDataBean> list_bean = new ArrayList<RepaymentHistoryDataBean>();
	/**
	 * 通过接口拿回的数据，用于将其转换为list_bean
	 */
	private List<CalendarListDataItem> datalist = new ArrayList<CalendarListDataItem>();

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				CalendarListData data = (CalendarListData) msg.obj;
				if (data != null) {
					pageCount = data.getPageCount();
					if (pageNO > pageCount) {
						mXListView.stopLoadMore(-1);
						if (REFRESHFLAG) {
							mXListView.stopLoadMore(1);
						}
					} else {
						mXListView.setNoMore(false);
						if (pageNO == 1) {
							datalist.clear();
						}
						datalist.addAll(data.getData());
						list_bean.clear();
						list_bean.addAll(RepaymentHistoryDataBean
								.getDataList(datalist));
						mAdapter.notifyDataSetChanged();
						if (data.getData().size() < pageSize) {
							mXListView.stopLoadMore(1);
						} else {
							mXListView.stopLoadMore(0);
						}
					}
					mXListView.stopRefresh();
					mXListView.initState();
				} else {
					mXListView.stopLoadMore(1);
					mXListView.stopRefresh();
				}

				if (REFRESHFLAG) {
					if (datalist == null || datalist.size() == 0) {
						mNullImg.setVisibility(View.VISIBLE);
						mNullTv.setVisibility(View.VISIBLE);
						mNullTv.setText("暂无任何还款计划");
						mNullImg.setImageResource(R.drawable.img_null);
					} else {
						mNullImg.setVisibility(View.GONE);
						mNullTv.setVisibility(View.GONE);
					}
				}

				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;

			case CODE_ELSE:
				mXListView.stopLoadMore(1);
				mXListView.stopRefresh();

				if (datalist == null || datalist.size() == 0) {
					mNullImg.setImageResource(R.drawable.img_null_net);
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						mNullTv.setText("网络错误，请稍候下拉刷新重试");
					} else {
						mNullTv.setText("服务异常，请稍候下拉刷新重试");
					}
					mNullImg.setVisibility(View.VISIBLE);
					mNullTv.setVisibility(View.VISIBLE);
				}

				if (isShowToastOnRefresh) {
					String desc = (String) msg.obj;
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mContext, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				break;

			default:
				break;
			}
		}
	};

	public RepaymentHistoryView(Context context) {
		super(context);
		mContext = context;
		initView();
	}

	public RepaymentHistoryView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		initView();
	}

	public RepaymentHistoryView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		initView();
	}

	private void initView() {
		View view = LayoutInflater.from(mContext).inflate(
				R.layout.layout_view_repaymenthistory, null);
		addView(view, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		mNullImg = (ImageView) view.findViewById(R.id.id_no_content);
		mNullTv = (TextView) view.findViewById(R.id.id_no_content_text);
		mXListView = (KkdXlistView) view.findViewById(R.id.mXListView);
		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setDividerHeight(0);
		mAdapter = new RepaymentHistoryAdapter(mContext, list_bean);
		mListView.setAdapter(mAdapter);
		mXListView.startRefresh();
		requestData();
	}

	/**
	 * 请求接口数据
	 */
	private void requestData() {
		KkdHttpMagager.getInstance().getCalendarList(pageNO, pageSize,
				CalendarListData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						Message msg = Message.obtain();
						msg.what = CODE_ZERO;
						msg.obj = responseBody;
						mHandler.sendMessage(msg);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = CODE_ELSE;
						msg.obj = desc;
						msg.arg1 = statusCode;
						mHandler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		requestData();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestData();
	}
}
